package com.aiyan.edu.service.learning.task;

import com.aiyan.edu.framework.domain.task.Task;
import com.aiyan.edu.framework.model.response.ResponseResult;
import com.aiyan.edu.service.learning.config.RabbitmqConfig;
import com.aiyan.edu.service.learning.service.CourseLearningService;
import com.alibaba.fastjson.JSON;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 通知选课任务
 *
 * @author: zqzhou
 * @create: 2019/11/24 16:09
 **/
@Component
public class ChooseCourseTask {

    @Autowired
    CourseLearningService courseLearningService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 发送选课消息，通知选课
     */
    @RabbitListener(queues = RabbitmqConfig.QUEUE_LEARNING_ADDCHOOSECOURSE)
    public void doChoose(Task task) {
        String requestBody = task.getRequestBody();
        Map map = JSON.parseObject(requestBody, Map.class);
        String userId = map.get("userId").toString();
        String courseId = map.get("courseId").toString();

        ResponseResult responseResult = this.courseLearningService.choseCourse(userId, courseId, null, null, null, task);

        // 选课添加成功，发送消息告知选择成功
        if (responseResult.isSuccess()) {
            this.rabbitTemplate.convertAndSend(RabbitmqConfig.EX_LEARNING_ADDCHOOSECOURSE, RabbitmqConfig.ROUTING_LEARNING_FINISHADDCHOOSECOURSE_KEY, task);
        }
    }
}
